package com.cn.transfer.handle.impl.AaPartner;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cn.transfer.common.constant.KeyConstant;
import com.cn.transfer.common.entity.DocumentApproval;
import com.cn.transfer.common.entity.VvAaPartner;
import com.cn.transfer.common.entity.VvPuPurchaseArrival;
import com.cn.transfer.common.entity.VvPuPurchaseArrivalB;
import com.cn.transfer.common.http.ApiClient;
import com.cn.transfer.common.utils.ConnmeUtils;
import com.cn.transfer.common.vo.MessagePushVO;
import com.cn.transfer.handle.MessageStrategy;
import com.cn.transfer.mapper.VvAaPartnerMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2023/11/14 16:44
 * @Description: 往来单位新增数据添加
 */
@Slf4j
@Component
public class AaPartnerInsertImpl implements MessageStrategy {
    @Autowired
    private VvAaPartnerMapper vvAaPartnerMapper;

    @Override
    public void handle(MessagePushVO message) {
        DocumentApproval documentApproval = ConnmeUtils.mapToObject(message.getBizContent(), DocumentApproval.class);
        VvAaPartner vvAaPartner = vvAaPartnerMapper.byCode(documentApproval.getCode());
        // 查询视图，这条数据是否存在
        int i1 = vvAaPartnerMapper.inquireIsExist(vvAaPartner.getCode());
        if (i1 == 0) {
            // 存入视图
            vvAaPartnerMapper.insert(vvAaPartner);
        }
        Header[] originalHeaders = ConnmeUtils.createHeaders();
        Map<String, Object> parameter = new HashMap<>();
        parameter.put("app_id", "6551cc3c1e701c4474516028");
        parameter.put("entry_id", "655331ce9b274035b146ec44");
        parameter.put("data", ConnmeUtils.convertEntityToMap(vvAaPartner));
        String jsonSale = JSONUtil.toJsonStr(parameter);
        log.info("往来单位新增时发生的数据：==== " + jsonSale);
        JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/create", originalHeaders, jsonSale);
        log.info("往来单位新增时接收的数据：====" + jdySale);
    }

    @Override
    public String getMsgType() {
        return "Partner_Create";
    }
}
